﻿using System;
using System.Collections;
using System.Data;
//using System.Runtime.CompilerServices;
//using System.Runtime.InteropServices;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;


namespace DotNetNuke.Modules.UserDefinedTable
{



  public class DataTypeUrlDocument : DataType
  {




    protected struct FieldSettings
    {
      public string Title;
      public string OutputFormat;
      public bool Abbreviate;
    }




    public override void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noScript)
    {
      if (ds != null)
      {
        FieldSettings field;
        ArrayList fields = new ArrayList();
        DataTable tableData = ds.Tables[DataSetTableName.Data];
        DotNetNuke.Modules.UserDefinedTable.TokenReplace objTokenReplace = new DotNetNuke.Modules.UserDefinedTable.TokenReplace();
        objTokenReplace.ModuleId = ModuleId;

        foreach (DataRow row in ds.Tables[DataSetTableName.Fields].Rows)
        {
          if (Convert.ToString(row[FieldsTableColumn.Type]) == this.Name)
          {
            field = new FieldSettings();
            field.Title = Convert.ToString(row[FieldsTableColumn.Title]);
            field.OutputFormat = Utilities.CStrN((row[FieldsTableColumn.OutputSettings]), "");
            field.Abbreviate = Utilities.CBoolN((row[FieldsTableColumn.NormalizeFlag]), false);
            fields.Add(field);
            tableData.Columns.Add(new DataColumn(field.Title + DataTableColumn.Appendix_Url, typeof(string)));
            tableData.Columns.Add(new DataColumn(field.Title + DataTableColumn.Appendix_Caption, typeof(string)));
            tableData.Columns.Add(new DataColumn(field.Title + DataTableColumn.Appendix_Original, typeof(string)));
          }
        }


        if (fields.Count >= 1)
        {
          bool bolNewWindowSetting;

          PortalSettings PortalSettings = PortalController.GetCurrentPortalSettings();
          Hashtable settings = new ModuleController().GetModuleSettings(ModuleId);

          if (!settings.ContainsKey(SettingName.URLNewWindow))
            bolNewWindowSetting = false;
          
          else
            bolNewWindowSetting = bool.Parse(Convert.ToString(settings[SettingName.URLNewWindow]));
          
          FileController fc = new FileController();
          string rawDBValue;
          UrlDocumentObject DocObj;

          foreach (DataRow row in tableData.Rows)
          {
            foreach (FieldSettings fld in fields)
            {
              rawDBValue = Convert.ToString(row[fld.Title]);

              if (!string.IsNullOrEmpty(rawDBValue))
              {
                DocObj = new UrlDocumentObject(ModuleId, rawDBValue);
                row[fld.Title] = (DocObj.ToLink());
                row[fld.Title + DataTableColumn.Appendix_Caption] = fld.Title;
                row[fld.Title + DataTableColumn.Appendix_Original] = DocObj.FileID;
                row[fld.Title + DataTableColumn.Appendix_Url] = DocObj.ToUrl();
              }
            }

          }

        }
      }
    }




    public override DotNetNuke.Modules.UserDefinedTable.EditControl EditControl
    { get { return new EditUrlDocument(); } }





    public override string Name
    { get { return "UrlDocument"; } }





    public override bool SupportsEditing
    { get { return true; } }




    public override bool SupportsNormalizeFlag
    { get { return true; } }




    public override bool SupportsValidation
    { get { return true; } }


  }

}

